package com.lock.immersive.controller;

import com.lock.immersive.annotation.AuthCheck;
import com.lock.immersive.exception.BusinessException;
import com.lock.immersive.mapper.AddressMapper;
import com.lock.immersive.pojo.domain.Address;
import com.lock.immersive.pojo.dto.AddressDto;
import com.lock.immersive.service.AddressService;
import com.lock.immersive.utils.ErrorCode;
import com.lock.immersive.utils.ResponseApi;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;


@RestController
@RequestMapping("/address")
public class AddressController {
    @Resource
    public AddressService addressService;
    @Resource
    public AddressMapper addressMapper;

    @PostMapping("/insertAddress")
    @AuthCheck
    public ResponseApi<Object> insertAddress(@RequestBody AddressDto addressDto, HttpServletRequest request) {
        return addressService.insertAddress(addressDto, request);
    }

    @GetMapping("/getAddress")
    @AuthCheck
    public ResponseApi<List<Address>> getAddress(HttpServletRequest request) {
        return addressService.getAddress(request);
    }

    @PostMapping("/deleteAddressList")
    @AuthCheck
    public ResponseApi<Object> deleteAddress(@RequestBody List<String> ids) {
        int i = addressMapper.deleteBatchIds(ids);
        if (i > 0) {
            return ResponseApi.success("删除成功");
        } else {
            throw new BusinessException(ErrorCode.OPERATION_ERROR);
        }
    }
}
